package com.coocaa.ops.admin.v1.timedtask.job.util;

import com.coocaa.ops.common.core.biz.timedtask.entity.TimedTaskInfoEntity;
import org.apache.commons.lang3.StringUtils;

import java.util.UUID;

/**
 * @author bijiahao
 * @date : 2017/12/18.
 * @description
 */
public class MyJobUtils {

    /**
     * 获取组名称
     *
     * @param entity
     * @return
     */
    public static String getGroupName(TimedTaskInfoEntity entity) {
        return entity.getTimedType();
    }


    /**
     * 获取定时任务 名称
     *
     * @param timedTask
     * @return
     */
    public static String getJobName(TimedTaskInfoEntity timedTask) {
        String jobName = "Job-";
        return getNameByPrefix(jobName, timedTask);
    }

    /**
     * 获取定时触发器名称
     *
     * @param timedTask
     * @return
     */
    public static String getTriggerName(TimedTaskInfoEntity timedTask) {
        String triggerName = "Trigger-";
        return getNameByPrefix(triggerName, timedTask);

    }

    private static String getNameByPrefix(String prefix, TimedTaskInfoEntity timedTask) {
        if (StringUtils.isBlank(prefix)) {
            prefix = "default-";
        }
        if (null == timedTask) {
            return prefix + UUID.randomUUID().toString();
        }
        return prefix + timedTask.getTimedType() + "-" + timedTask.getResourceType() + "-" + timedTask.getResourceId() + "-" + timedTask.getResourceVersion();
    }


}
